/*
 * $Id: MyApplicationListener.java 42 2013-04-17 15:33:59Z gabakyan $
 * $Author: gabakyan $
 * $Revision: 42 $
 * $Date: 2013-04-17 15:33:59 +0000 (Wed, 17 Apr 2013) $
 *
 * Copyright (c) 2013 Supply Chain Intelligence (SCI), Inc.
 * http://www.scintelligence.com/, Email: info@scintelligence.com
 * All rights reserved.
 *
 * This file is part of Logistics Map.
 *
 * Logistics Map is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * Logistics Map is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Logistics Map.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sci.logisticsmap.service.support;

import com.sci.logisticsmap.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.Locale;
import java.util.Map;

/**
 * The class <code>MyApplicationListener</code> represents the application event listener.
 *
 * @author gabakyan@gmail.com (Last modified by $Author: gabakyan $)
 * @version $Revision: 42 $
 */
@Component
public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
    /**
     * The logger.
     */
	private static Logger logger;

    /**
     * The static initialization block.
     */
    static {
	    logger = LoggerFactory.getLogger(MyApplicationListener.class);
    }

//    /**
//     * The geodata service.
//     */
//    @Autowired
//    private GeodataService geodataService;
    /**
     * The resource bundle message service.
     */
    @Autowired
    private ResourceBundleMessageService resourceBundleMessageService;
    /**
     * The runtime configuration service.
     */
    @Autowired
    private RuntimeConfigurationService runtimeConfigurationService;
    /**
     * The message source.
     */
    @Autowired
    private MyReloadableResourceBundleMessageSource messageSource;

    /**
     * The default constructor.
     */
    public MyApplicationListener() {
        logger.debug("Application context listener is created");
    }

    /*
     * (non-Javadoc)
     *
     * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
     */
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        logger.debug("Application context \"{}\" is ready for use", event.getApplicationContext().getDisplayName());

        initializeEditedMessages();

        runtimeConfigurationService.initInstance();
        //geodataService.deleteAll();
    }

    /**
     * Initializes the edited messages.
     */
    private void initializeEditedMessages() {
        Map<Locale, Map<String, String>> editedMessages = resourceBundleMessageService.getEditedMessages();
        messageSource.setEditedMessages(editedMessages);
    }
}
